using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace MobileTDL
{
    public class ToDoListFile : DocumentBase
    {
        private ToDoList _toDoList;

        public ToDoListFile(XmlDocument doc)
            : base(doc)
        {
        }

        public ToDoListFile(string filename)
            : base(filename)
        {
        }

        public ToDoListFile()
            : base(new XmlDocument())
        {
        }

        public ToDoList ToDoList
        {
            get
            {
                if (_toDoList == null)
                {
                    if (Doc.DocumentElement == null) Doc.AppendChild(Doc.CreateElement("TODOLIST"));
                    _toDoList = new ToDoList(Doc, Doc.DocumentElement);
                }
                return _toDoList;
            }
        }

        public void Save(string filename)
        {
            // save without Byte-Order Mark so that desktop ToDo List can read the file.
            StreamWriter tw = new StreamWriter(filename, false,
                new UTF8Encoding(false));
            Doc.Save(tw);
            tw.Close();

            // Doc.Save(filename);
        }
    }
}
